package clothes.tools.web;

import clothes.tools.pojo.Customer;
import clothes.tools.service.CustomerService;
import clothes.tools.service.impl.CustomerServiceImp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

public class CustomerServlet extends BaseServlet{
    private CustomerService customerService=new CustomerServiceImp();

    /**
     * 显示所有用户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageStr=req.getParameter("page");
        String limitStr=req.getParameter("limit");
        List<Customer> customer = customerService.queryCustomer();
        List<Customer> customers=customerService.queryCustomer(pageStr,limitStr);
        resp.setContentType ("text/html;charset=UTF-8");
        JSONObject jsonObject = new JSONObject ();
        //接收的数据
        JSONArray data = JSONArray.parseArray (JSON.toJSONString (customers));
        jsonObject.put ("data", data);
        jsonObject.put ("code", 0);
        jsonObject.put ("msg", "");
        //获得数据总数
        jsonObject.put ("count", customer.size ());
        PrintWriter writer = resp.getWriter ();
        writer.print (jsonObject.toJSONString ());
    }

    /**
     * 删除用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        customerService.deleteById(cid);
        resp.getWriter().write("success");
    }

    /**
     * 更新用户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String cid = req.getParameter("cid");
        String mima = req.getParameter("mima");
        String gender = req.getParameter("gender");
        String balance = req.getParameter("balance");
        String emile = req.getParameter("e_mile");
        customerService.updateCustomer(new Customer(cid,mima,gender,new BigDecimal(balance),emile));
        resp.getWriter().write("success");
    }

    /**
     * 查询用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String pageStr=req.getParameter("page");
        String limitStr=req.getParameter("limit");
        String select = req.getParameter("select");
        List<Customer> customer = customerService.queryCustomer();
        List<Customer> customers=customerService.queryCustomers(pageStr,limitStr,select);
        resp.setContentType ("text/html;charset=UTF-8");
        JSONObject jsonObject = new JSONObject ();
        JSONArray data = JSONArray.parseArray (JSON.toJSONString (customers));
        jsonObject.put ("data", data);
        jsonObject.put ("code", 0);
        jsonObject.put ("msg", "");
        jsonObject.put ("count", customer.size());
        PrintWriter writer = resp.getWriter ();
        writer.print (jsonObject.toJSONString ());
    }
}
